// gjw/pages/weipai/paimai.js

const app = getApp()
var dataheith = [0];
var activeIndex = 0;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    tabMenu: ['推荐', '拍卖中','分类','预展'],
    addIndex:0,
    activeNav:0,
    menu: [],
    noData: true,
    noDatas:true,
    list: [],
    activeIndex:0,
    index:0,
    lists: [],

    tab_config: {
      tabs: [],
      tab_left: 0,
    },
    scroll: "scroll"
  },

  // once: function (e) {
  //   var n = 0;
  //   var that = this
  //   var show = false
  //   var hide = true
  //   that.setData({
  //     activeIndex: n,
  //     noData:hide
  //   })

  //   if (that.data.activeNav==0){
  //     that.paimaiOnce()      
  //   } else if (that.data.activeNav == 1){
  //     that.paimaiTwice()
  //   }
  // },
  
  tab:function(e){       //tab选项卡切换
    
    var that=this
    var menu = that.data.menu
    var show = false
    var hide = true
    var p_id=menu[0].id
    that.setData({
      activeNav: e.currentTarget.dataset.addindex,
      activeIndex:0,
      noData:hide
    })
    if (e.currentTarget.dataset.addindex==0) {
      that.paimaiOnce()
    } else if (e.currentTarget.dataset.addindex == 1){
      this.all_paimai()
    } else if (e.currentTarget.dataset.addindex == 3){
      app.util.request({
        url: 'entry/wxapp/Shotwillcateid',
        data: { co_id: p_id },
        success: function (res) {
          
          if (res.data.code == 500) {
            that.setData({
              noDatas: show,
            })
          } else if (res.data.code = 200) {
            var lists = res.data.res
            for (let i in lists) {
              lists[i].img = lists[i].img.split(',')
              lists[i].fromtime = lists[i].fromtime.slice(5, 19)

            }
            

            that.setData({
              noDatas: hide,
              lists: lists
            })
          }
        }
      })
    }
  },

  // tabbar.js
  tabbar: function (e) {
    
    var that = this
    var url = e.currentTarget.dataset.url
    wx.reLaunch({
      url: url,
    })
  },

  // 拍卖中全部
  all_paimai:function(e){
    var that = this
    var show = false
    var hide = true
    app.util.request({
      url: 'entry/wxapp/Shotcateid',
      success: function (res) {
        
        if (res.data.code == 500) {
          that.setData({
            noData: show
          })
        } else if (res.data.code = 200) {
          var list = res.data.res
          for (let i in list) {
            list[i].img = list[i].img.split(',')
          }
          

          that.setData({
            noData: hide,
            list: list
          })
        }
      }
    })
  },


  tabClick: function (e) {       //点击分类
    
    var that=this
    var show = false
    var hide=true
    var p_id=e.currentTarget.dataset.id
    that.setData({
      sliderOffset: e.currentTarget.dataset.offsetLeft,
      activeIndex: e.currentTarget.dataset.index,
      p_id: p_id
    })
      app.util.request({
        url: 'entry/wxapp/Shotwillcateid', 
        data: { co_id: p_id },
        success: function (res) {
          
          if (res.data.code == 500) {
            that.setData({
              noDatas: show,
            })
          } else if (res.data.code = 200) {
            var lists = res.data.res
            for (let i in lists) {
              lists[i].img = lists[i].img.split(',')
              lists[i].fromtime = lists[i].fromtime.slice(5, 19)
            
            }
            
            
            that.setData({
              noDatas: hide,
              lists: lists
            })
          }
        }
      })
    
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    app.route(this)
		var url = wx.getStorageSync('siteUrl')
    app.location_attachurl(that)
    that.setData({
      siteUrl: url
    })
    that.wpfenlei()
    that.paimaiOnce()
    // that.paimaiTwice()
    that.wp_three()
  },



  // 微拍分类获取
  wpfenlei: function (e) {
    var that = this
    app.util.request({
      url: 'entry/wxapp/catestop',
      success: function (res) {
        
        var menu = res.data.res
        that.setData({
          menu: menu
        })
      }
    })
  },

  // 地摊分类
  wp_three: function (e) {
    var that = this
    app.util.request({
      url: 'entry/wxapp/catestopdw',
      success: function (res) {
        
        var array = res.data.res
        // that.setData({
        //   list: array
        // })
        var typelist = [];
        for (let i = 0; i < array.length; i++) {
          if (array[i].arr.length > 0) {
            typelist.push(array[i])
          }
        }
        // console.log(typelist)
        var indexheight = 0
        for (var i = 0; i < typelist.length; i++) {
          indexheight += (Math.floor(typelist[i].arr.length / 3) + 1) *100;
          dataheith.push(indexheight);
        }
        //
        let { window_height, tab_config } = that.data;
        // var sjxx = wx.getSystemInfoSync()
        wx.getSystemInfo({
          success: function (res) {
            that.setData({
              // tab_config: {
              //   tabs: array,
              // },
              windowHeight: res.windowHeight
            })
          }
        })
        // console.log(sjxx) 
        window_height = that.data.windowHeight - 92

        // tab_config.item_height = window_height / tab_config.tabs.length;
        tab_config.item_height = 50

        that.setData({ "window_height": window_height, "tab_config": tab_config });
        that.setData({
          tab_config: {
            tabs: typelist,
            tab_left: 0,
          },
        })
      }

    })
  },


  catItemClick: function (e) {
    var that = this;
    var activeIndex = that.data.activeIndex
    // that.updateSelectedPage(parseInt(activeIndex));
    this.setData({
      intoid: "id" + e.currentTarget.dataset.index,
      activeIndex: e.currentTarget.dataset.index,
      scroll: ""
    });
    setTimeout(function () {
      that.setData({
        scroll: "scroll"
      });
    }, 500)
  },

  // 滚动触发
  scroll: function (e) {
    
    var that = this;
    var scrolltop = e.detail.scrollTop;
    var scrollbotton = e.detail.scrollTop
    for (var i = 0; i < dataheith.length; i++) {
      if (scrolltop <= dataheith[i - 1]) {
        if (activeIndex != i) {
          that.updateSelectedPage(i - 2);
          activeIndex = i - 2
          if (activeIndex == -1) {
            this.setData({
              activeIndex: 0
            })
            return
          }
          this.setData({
            activeIndex: activeIndex
          })
        }
        break
      }
    }
  },


  // 更换页面到指定page ，从0开始
  updateSelectedPage(page) {
    let that = this;
    let { window_height, tab_config } = this.data;
    let underline_offset = tab_config.item_width * page;

    let show_item_num = Math.round(window_height / tab_config.item_height);
    let min_left_item = tab_config.item_height * (page - show_item_num + 1);
    let max_left_item = tab_config.item_height * page;
    if (tab_config.tab_left < min_left_item || tab_config.tab_left > max_left_item) {
      tab_config.tab_left = max_left_item - (window_height - tab_config.item_height) / 2;
    }
    that.setData({
      "tab_config": tab_config,
    });
  },





  
// 推荐数据获取

  paimaiOnce: function (e) {
    var that = this
    var hide = true
    var show=false
    app.util.request({
      url: 'entry/wxapp/Shotupfee',
      success: function (res) {
        
       if(res.data.code==500){
         that.setData({
           noData:show
         })

       }else if(res.data.code==200){
         var list = res.data.res
         for (let i in list) {
           list[i].img = list[i].img.split(',')
         }
         
         that.setData({
           list: list,
           noData: hide
         })
       }
      }
    })
  },


  // 预展推荐数据获取

  paimaiTwice: function (e) {
    var hide = true
    var show = false
    var that = this
    app.util.request({
      url: 'entry/wxapp/Shotwillupfee',
      success: function (res) {
        
        if (res.data.code == 500) {
          that.setData({
            noDatas: show
          })

        } else if (res.data.code == 200) {
          var lists = res.data.res
          for (let i in lists) {
            lists[i].img = lists[i].img.split(',')

            lists[i].fromtime = lists[i].fromtime.slice(5,19)
          }
          
          that.setData({
            noDatas:hide,
            lists: lists
          })
        }
       
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var activeNav = that.data.activeNav
    if (activeNav == 0) {
      that.paimaiOnce()
    } else if (activeNav==1){
      that.all_paimai()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that=this
    var show = false
    var hide=true
    var activeNav = that.data.activeNav
    var activeindex = that.data.activeIndex
    var menu = that.data.menu
    if (activeNav==0){       //拍卖中 推荐  
      that.paimaiOnce()
    }else if(activeNav==1){
    //  拍卖中
      that.all_paimai() 
    } else if (activeNav == 3) {
      //  预展
      var fl_id_data = menu[activeindex].id
      app.util.request({
        url: 'entry/wxapp/Shotwillcateid',
        data: { co_id: fl_id_data },
        success: function (res) {
          
          if (res.data.code == 500) {
            that.setData({
              noDatas: show,
            })
          } else if (res.data.code = 200) {
            var lists = res.data.res
            for (let i in lists) {
              lists[i].img = lists[i].img.split(',')
              lists[i].fromtime = lists[i].fromtime.slice(5, 19)

            }
            

            that.setData({
              noDatas: hide,
              lists: lists
            })
          }
        }
      })
    }
    
    wx.stopPullDownRefresh()

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    that.data.page = that.data.page + 1
    var activeNav = that.data.activeNav
    var activeIndex = that.data.activeIndex
		var openid = wx.getStorageSync('openid')
    if (activeNav==0){
      app.util.request({
        'url': 'entry/wxapp/Shotupfee',
        'cachetime': '0',
        data: { page: that.data.page },
        success: function (res) {
          if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          } else if (res.data.code == 200) {
            for (let j in res.data.res) {
              res.data.res[j].img = res.data.res[j].img.split(',')
            }
            for (var i = 0; i < res.data.res.length; i++) {
              that.data.list.push(res.data.res[i])
            }
            that.setData({
              list: that.data.list
            })
          }
        }
      })
    } else if (activeNav == 1) {
      var c_id = that.data.p_id
      app.util.request({
        'url': 'entry/wxapp/Shotcateid',
        'cachetime': '0',
        data: { page: that.data.page, },
        success: function (res) {
          
          if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          } else if (res.data.code == 200) {

            for (let j in res.data.res) {
              res.data.res[j].img = res.data.res[j].img.split(',')
            }
            for (var i = 0; i < res.data.res.length; i++) {
              that.data.list.push(res.data.res[i])
            }
            that.setData({
              list: that.data.list
            })
          }
        }
      })
    }else if(activeNav==3){
        var that = this
        var c_id = that.data.p_id
        app.util.request({
          'url': 'entry/wxapp/Shotwillcateid',
          'cachetime': '0',
          data: { co_id: c_id, page: that.data.page, },
          success: function (res) {
            
            if (res.data.code == 500) {
              wx.showToast({
                title: '没有新的数据了！',
                icon: 'none'
              })
            } else if (res.data.code == 200) {

              for (let j in res.data.res) {
                res.data.res[j].img = res.data.res[j].img.split(',')
              }
              for (var i = 0; i < res.data.res.length; i++) {
                that.data.lists.push(res.data.res[i])
              }
              that.setData({
                lists: that.data.lists
              })
            }
          }
        })
      }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})